/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.config.javabeans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.config.TriggersConfig;
import opiam.admin.faare.config.javabeans.JBTrigger;
import opiam.admin.faare.config.javabeans.JBTriggers;
import opiam.admin.faare.service.services.triggers.TriggersService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * Test of the JBTriggers class.
 *
 */
public class JBTriggersTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBTriggersTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Triggers configuration file. */
    public static final String TRIGGERS_XML_FILE = "/config/service/triggers/triggers_conf.xml";

    /** Instance of JBTriggers. */
    private static JBTriggers jbTriggersLoaded = new JBTriggers();

    /**
     * Creates a new JBTriggersTest object.
     *
     * @param arg0  Parameter.
     */
    public JBTriggersTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
            jbTriggersLoaded = TriggersConfig.getInstance().readConfig(TriggersService.class.getResourceAsStream(
                        TRIGGERS_XML_FILE));
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBTriggersTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBTriggersTest("testBegin"));

        suite.addTest(new JBTriggersTest("testAddPreTrigger"));
        suite.addTest(new JBTriggersTest("testAddPostTrigger"));

        suite.addTest(new JBTriggersTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBTriggersTest");

        _test.displayHeaderClass("JBTriggersTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBTriggersTest");
    }

    /**
     * Test the methods: addPreTrigger and getPreTriggers.
     *
     */
    public void testAddPreTrigger()
    {
        _test.displayHeaderMethod("testAddPreTrigger");

        try
        {
            JBTrigger jbTrigger4 = new JBTrigger();

            jbTrigger4.setName("test_trigger4");
            jbTrigger4.setPost(false);
            jbTrigger4.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");
            jbTrigger4.setOpname("LOAD");
            jbTrigger4.addParam("mail.smtp.sender.template",
                "tests_unitaires@opiam.org");
            jbTrigger4.addParam("mail.smtp.recipient.template",
                "tests_unitaires@opiam.org");
            jbTrigger4.addParam("mail.smtp.subject.template",
                "PRE test de uid=prose, ou=People, dc=mycompany,dc=com");
            jbTrigger4.addParam("mail.smtp.body.filename",
                "/config/service/triggers/post_body_template_create.vm");
            jbTrigger4.addParam("mail.smtp.host", "mailmir");

            Map resultPreTriggersMap = new HashMap(jbTriggersLoaded.getPreTriggersMap());
            Map expectedPreTriggersMap = jbTriggersLoaded.getPreTriggersMap();

            int expectedSize1 = expectedPreTriggersMap.size();
            expectedPreTriggersMap.put("test_trigger4", jbTrigger4);

            int expectedSize2 = expectedPreTriggersMap.size();

            int resultSize1 = resultPreTriggersMap.size();
            jbTriggersLoaded.addPreTrigger(jbTrigger4);
            resultPreTriggersMap = jbTriggersLoaded.getPreTriggersMap();

            int resultSize2 = resultPreTriggersMap.size();

            if (expectedSize2 == resultSize2)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddPreTrigger");
    }

    /**
     * Test the methods: addPostTrigger and getPostTriggers.
     *
     */
    public void testAddPostTrigger()
    {
        _test.displayHeaderMethod("testAddPostTrigger");

        try
        {
            JBTrigger jbTrigger4 = new JBTrigger();

            jbTrigger4.setName("test_trigger4");
            jbTrigger4.setPost(true);
            jbTrigger4.setClassname(
                "opiam.admin.faare.service.services.triggers.MailSenderTrigger");
            jbTrigger4.setOpname("LOAD");
            jbTrigger4.addParam("mail.smtp.sender.template",
                "tests_unitaires@opiam.org");
            jbTrigger4.addParam("mail.smtp.recipient.template",
                "tests_unitaires@opiam.org");
            jbTrigger4.addParam("mail.smtp.subject.template",
                "PRE test de uid=prose, ou=People, dc=mycompany,dc=com");
            jbTrigger4.addParam("mail.smtp.body.filename",
                "/config/service/triggers/post_body_template_create.vm");
            jbTrigger4.addParam("mail.smtp.host", "mailmir");

            Map resultPostTriggersMap = new HashMap(jbTriggersLoaded.getPostTriggersMap());
            Map expectedPostTriggersMap = jbTriggersLoaded.getPostTriggersMap();

            int expectedSize1 = expectedPostTriggersMap.size();
            expectedPostTriggersMap.put("test_trigger4", jbTrigger4);

            int expectedSize2 = expectedPostTriggersMap.size();

            int resultSize1 = resultPostTriggersMap.size();
            jbTriggersLoaded.addPostTrigger(jbTrigger4);
            resultPostTriggersMap = jbTriggersLoaded.getPostTriggersMap();

            int resultSize2 = resultPostTriggersMap.size();

            if (expectedSize2 == resultSize2)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddPostTrigger");
    }
}
